package com.woniuxy.yoga.course.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.course.dao.model.AloneReservation;
import com.woniuxy.yoga.course.model.dto.alone.AloneReservationDTO;
import com.woniuxy.yoga.course.model.form.alone.AddAloneReservationForm;
import com.woniuxy.yoga.course.model.form.alone.CoachQueryMyAloneReservationForm;
import com.woniuxy.yoga.course.model.form.alone.UserQueryMyAloneReservationForm;
import com.woniuxy.yoga.course.model.param.alone.AddAloneReservationParam;
import com.woniuxy.yoga.course.model.param.alone.CoachQueryMyAloneReservationParam;
import com.woniuxy.yoga.course.model.param.alone.UserQueryMyAloneReservationParam;
import com.woniuxy.yoga.course.service.service.AloneReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 私教预约表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/aloneReservation")
@Api(tags = "私教预约课程相关业务接口")
@Validated
public class AloneReservationController {

    @Autowired
    private AloneReservationService aloneReservationService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 教练ID查询预约信息
     * @param ids
     * @return
     */
    @PostMapping("/getReservationByCoachId")
    @ApiOperation("教练ID查询预约信息接口")
    public Result<List<AloneReservationDTO>> getReservationByCoachId(@RequestBody List<Integer> ids){
        //获取数据
        List<AloneReservationDTO> aloneReservationDTOS = aloneReservationService.getReservationByCoachId(ids);
        //返回数据
        return Result.getListResult(AloneReservationDTO.class).setMessage("根据教练ID查询预约信息成功").setData(aloneReservationDTOS);
    }

    /**
     * 根据私教预约ID查询私教预约信息
     * @param reservationId
     * @return
     */
    @GetMapping("/getById/{reservationId}")
    @ApiOperation("ID查询私教预约信息接口")
    @ApiImplicitParam(name = "reservationId",value = "私教预约ID")
    public Result<AloneReservationDTO> getById(@Valid @NotNull(message = "私教预约ID不能为空") @PathVariable("reservationId") Integer reservationId){
        //获取数据
        AloneReservation aloneReservation = aloneReservationService.getById(reservationId);
        //对象转换
        AloneReservationDTO aloneReservationDTO = BeanUtil.toBean(aloneReservation, AloneReservationDTO.class);
        //返回数据
        return Result.getResult(AloneReservationDTO.class).setMessage("根据私教预约ID查询私教预约信息成功").setData(aloneReservationDTO);
    }

    /**
     * 新增私教预约信息
     * @param addAloneReservationForm
     * @param authenToken
     * @return
     */
    @PostMapping("/addAloneReservation.us")
    @ApiOperation("新增私教预约信息")
    public Result<?> addAloneReservation(@Valid @RequestBody AddAloneReservationForm addAloneReservationForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取userId
        Integer userId = JWTUtil.getUserId(refreshToken);
        //对象转换
        AddAloneReservationParam param = BeanUtil.toBean(addAloneReservationForm, AddAloneReservationParam.class);
        //用户ID赋值
        param.setUserId(userId);
        //返回数据
        aloneReservationService.addAloneReservation(param);
        return Result.getResult().setMessage("新增私教预约信息成功");
    }

    /**
     * 私教预约次数减一
     * @param reservationId
     * @return
     */
    @GetMapping("/reduceAloneReservationCount/{reservationId}")
    @ApiOperation("私教预约次数减一接口")
    @ApiImplicitParam(name = "reservationId",value = "私教预约ID")
    public Result<?> reduceAloneReservationCount(@Valid @NotNull(message = "私教预约ID不能为空") @PathVariable("reservationId") Integer reservationId){
        aloneReservationService.reduceAloneReservationCount(reservationId);
        return Result.getResult().setMessage("私教预约次数减一成功");
    }

    /**
     * 私教预约确认
     * @param reservationId
     * @return
     */
    @GetMapping("/modifyAloneReservationConfirm/{reservationId}.co")
    @ApiOperation("私教预约确认接口")
    @ApiImplicitParam(name = "reservationId",value = "私教预约ID")
    public Result<?> modifyAloneReservationConfirm(@Valid @NotNull(message = "私教预约ID不能为空") @PathVariable("reservationId") Integer reservationId){
        aloneReservationService.modifyAloneReservationConfirm(reservationId);
        return Result.getResult().setMessage("私教预约确认成功");
    }

    /**
     * 私教预约拒绝
     * @param reservationId
     * @return
     */
    @GetMapping("/modifyAloneReservationRefuse/{reservationId}.co")
    @ApiOperation("私教预约拒绝接口")
    @ApiImplicitParam(name = "reservationId",value = "私教预约ID")
    public Result<?> modifyAloneReservationRefuse(@Valid @NotNull(message = "私教预约ID不能为空") @PathVariable("reservationId") Integer reservationId){
        aloneReservationService.modifyAloneReservationRefuse(reservationId);
        return Result.getResult().setMessage("私教预约拒绝成功");
    }

    /**
     * 教练查询自己的私教预约信息
     * @param coachQueryMyAloneReservationForm
     * @param authenToken
     * @return
     */
    @PostMapping("/coachQueryMyAloneReservation.co")
    @ApiOperation("教练查询私教预约信息接口")
    public Result<PageInfo> coachQueryMyAloneReservation(@RequestBody CoachQueryMyAloneReservationForm coachQueryMyAloneReservationForm, @RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取coachId
        Integer coachId = JWTUtil.getCoachId(refreshToken);
        //对象转换
        CoachQueryMyAloneReservationParam param = BeanUtil.toBean(coachQueryMyAloneReservationForm, CoachQueryMyAloneReservationParam.class);
        //教练ID赋值
        param.setCoachId(coachId);
        //返回数据
        PageInfo pageInfo = aloneReservationService.coachQueryMyAloneReservation(param);
        return Result.getResult(PageInfo.class).setMessage("教练查询自己的私教预约信息成功").setData(pageInfo);
    }

    /**
     * 用户查询自己的私教预约信息
     * @param userQueryMyAloneReservationForm
     * @param authenToken
     * @return
     */
    @PostMapping("/userQueryMyAloneReservation.us")
    @ApiOperation("用户查询私教预约信息接口")
    public Result<PageInfo> userQueryMyAloneReservation(@RequestBody UserQueryMyAloneReservationForm userQueryMyAloneReservationForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取userId
        Integer userId = JWTUtil.getUserId(refreshToken);
        //对象转换
        UserQueryMyAloneReservationParam param = BeanUtil.toBean(userQueryMyAloneReservationForm, UserQueryMyAloneReservationParam.class);
        //用户ID赋值
        param.setUserId(userId);
        //返回数据
        PageInfo pageInfo = aloneReservationService.userQueryMyAloneReservation(param);
        return Result.getResult(PageInfo.class).setMessage("用户查询自己的私教预约信息成功").setData(pageInfo);
    }
}


